<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>The “sites” framework &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="contrib packages" href="index.html" />
    <link rel="next" title="The staticfiles app" href="staticfiles.html" />
    <link rel="prev" title="The sitemap framework" href="sitemaps.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="sitemaps.html" title="The sitemap framework">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="staticfiles.html" title="The staticfiles app">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-sites">
            
  <div class="section" id="s-module-django.contrib.sites">
<span id="s-the-sites-framework"></span><span id="module-django.contrib.sites"></span><span id="the-sites-framework"></span><h1>The &#8220;sites&#8221; framework<a class="headerlink" href="#module-django.contrib.sites" title="Permalink to this headline">¶</a></h1>
<p>Django comes with an optional &#8220;sites&#8221; framework. It&#8217;s a hook for associating
objects and functionality to particular Web sites, and it&#8217;s a holding place for
the domain names and &#8220;verbose&#8221; names of your Django-powered sites.</p>
<p>Use it if your single Django installation powers more than one site and you
need to differentiate between those sites in some way.</p>
<p>The sites framework is mainly based on a simple model:</p>
<dl class="class">
<dt id="django.contrib.sites.models.Site">
<em class="property">class </em><tt class="descclassname">models.</tt><tt class="descname">Site</tt><a class="headerlink" href="#django.contrib.sites.models.Site" title="Permalink to this definition">¶</a></dt>
<dd><p>A model for storing the <tt class="docutils literal"><span class="pre">domain</span></tt> and <tt class="docutils literal"><span class="pre">name</span></tt> attributes of a Web site.
The <a class="reference internal" href="../settings.html#std:setting-SITE_ID"><tt class="xref std std-setting docutils literal"><span class="pre">SITE_ID</span></tt></a> setting specifies the database ID of the
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a> object (accessible using
the automatically added <tt class="docutils literal"><span class="pre">id</span></tt> attribute) associated with that
particular settings file.</p>
<dl class="attribute">
<dt id="django.contrib.sites.models.Site.domain">
<tt class="descname">domain</tt><a class="headerlink" href="#django.contrib.sites.models.Site.domain" title="Permalink to this definition">¶</a></dt>
<dd><p>The domain name associated with the Web site.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sites.models.Site.name">
<tt class="descname">name</tt><a class="headerlink" href="#django.contrib.sites.models.Site.name" title="Permalink to this definition">¶</a></dt>
<dd><p>A human-readable &#8220;verbose&#8221; name for the Web site.</p>
</dd></dl>

</dd></dl>

<p>How you use this is up to you, but Django uses it in a couple of ways
automatically via simple conventions.</p>
<div class="section" id="s-example-usage">
<span id="example-usage"></span><h2>Example usage<a class="headerlink" href="#example-usage" title="Permalink to this headline">¶</a></h2>
<p>Why would you use sites? It&#8217;s best explained through examples.</p>
<div class="section" id="s-associating-content-with-multiple-sites">
<span id="associating-content-with-multiple-sites"></span><h3>Associating content with multiple sites<a class="headerlink" href="#associating-content-with-multiple-sites" title="Permalink to this headline">¶</a></h3>
<p>The Django-powered sites <a class="reference external" href="http://www.ljworld.com/">LJWorld.com</a> and <a class="reference external" href="http://www.lawrence.com/">Lawrence.com</a> are operated by the
same news organization &#8211; the Lawrence Journal-World newspaper in Lawrence,
Kansas. LJWorld.com focuses on news, while Lawrence.com focuses on local
entertainment. But sometimes editors want to publish an article on <em>both</em>
sites.</p>
<p>The brain-dead way of solving the problem would be to require site producers to
publish the same story twice: once for LJWorld.com and again for Lawrence.com.
But that&#8217;s inefficient for site producers, and it&#8217;s redundant to store
multiple copies of the same story in the database.</p>
<p>The better solution is simple: Both sites use the same article database, and an
article is associated with one or more sites. In Django model terminology,
that&#8217;s represented by a <a class="reference internal" href="../models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> in the
<tt class="docutils literal"><span class="pre">Article</span></tt> model:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib.sites.models</span> <span class="kn">import</span> <span class="n">Site</span>

<span class="k">class</span> <span class="nc">Article</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">headline</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
    <span class="c"># ...</span>
    <span class="n">sites</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">Site</span><span class="p">)</span>
</pre></div>
</div>
<p>This accomplishes several things quite nicely:</p>
<ul>
<li><p class="first">It lets the site producers edit all content &#8211; on both sites &#8211; in a
single interface (the Django admin).</p>
</li>
<li><p class="first">It means the same story doesn&#8217;t have to be published twice in the
database; it only has a single record in the database.</p>
</li>
<li><p class="first">It lets the site developers use the same Django view code for both sites.
The view code that displays a given story just checks to make sure the
requested story is on the current site. It looks something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.sites.shortcuts</span> <span class="kn">import</span> <span class="n">get_current_site</span>

<span class="k">def</span> <span class="nf">article_detail</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">article_id</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">a</span> <span class="o">=</span> <span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="n">article_id</span><span class="p">,</span> <span class="n">sites__id</span><span class="o">=</span><span class="n">get_current_site</span><span class="p">(</span><span class="n">request</span><span class="p">)</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">Article</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="s">&quot;Article does not exist on this site&quot;</span><span class="p">)</span>
    <span class="c"># ...</span>
</pre></div>
</div>
</li>
</ul>
</div>
<div class="section" id="s-associating-content-with-a-single-site">
<span id="associating-content-with-a-single-site"></span><h3>Associating content with a single site<a class="headerlink" href="#associating-content-with-a-single-site" title="Permalink to this headline">¶</a></h3>
<p>Similarly, you can associate a model to the
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a>
model in a many-to-one relationship, using
<a class="reference internal" href="../models/fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>.</p>
<p>For example, if an article is only allowed on a single site, you&#8217;d use a model
like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib.sites.models</span> <span class="kn">import</span> <span class="n">Site</span>

<span class="k">class</span> <span class="nc">Article</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">headline</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
    <span class="c"># ...</span>
    <span class="n">site</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Site</span><span class="p">)</span>
</pre></div>
</div>
<p>This has the same benefits as described in the last section.</p>
</div>
<div class="section" id="s-hooking-into-the-current-site-from-views">
<span id="s-hooking-into-current-site-from-views"></span><span id="hooking-into-the-current-site-from-views"></span><span id="hooking-into-current-site-from-views"></span><h3>Hooking into the current site from views<a class="headerlink" href="#hooking-into-the-current-site-from-views" title="Permalink to this headline">¶</a></h3>
<p>You can use the sites framework in your Django views to do
particular things based on the site in which the view is being called.
For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">SITE_ID</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
        <span class="c"># Do something.</span>
        <span class="k">pass</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c"># Do something else.</span>
        <span class="k">pass</span>
</pre></div>
</div>
<p>Of course, it&#8217;s ugly to hard-code the site IDs like that. This sort of
hard-coding is best for hackish fixes that you need done quickly. The
cleaner way of accomplishing the same thing is to check the current site&#8217;s
domain:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.sites.shortcuts</span> <span class="kn">import</span> <span class="n">get_current_site</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">current_site</span> <span class="o">=</span> <span class="n">get_current_site</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">current_site</span><span class="o">.</span><span class="n">domain</span> <span class="o">==</span> <span class="s">&#39;foo.com&#39;</span><span class="p">:</span>
        <span class="c"># Do something</span>
        <span class="k">pass</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c"># Do something else.</span>
        <span class="k">pass</span>
</pre></div>
</div>
<p>This has also the advantage of checking if the sites framework is installed,
and return a <a class="reference internal" href="#django.contrib.sites.requests.RequestSite" title="django.contrib.sites.requests.RequestSite"><tt class="xref py py-class docutils literal"><span class="pre">RequestSite</span></tt></a> instance if
it is not.</p>
<p>If you don&#8217;t have access to the request object, you can use the
<tt class="docutils literal"><span class="pre">get_current()</span></tt> method of the <a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a>
model&#8217;s manager. You should then ensure that your settings file does contain
the <a class="reference internal" href="../settings.html#std:setting-SITE_ID"><tt class="xref std std-setting docutils literal"><span class="pre">SITE_ID</span></tt></a> setting. This example is equivalent to the previous one:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.sites.models</span> <span class="kn">import</span> <span class="n">Site</span>

<span class="k">def</span> <span class="nf">my_function_without_request</span><span class="p">():</span>
    <span class="n">current_site</span> <span class="o">=</span> <span class="n">Site</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_current</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">current_site</span><span class="o">.</span><span class="n">domain</span> <span class="o">==</span> <span class="s">&#39;foo.com&#39;</span><span class="p">:</span>
        <span class="c"># Do something</span>
        <span class="k">pass</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c"># Do something else.</span>
        <span class="k">pass</span>
</pre></div>
</div>
</div>
<div class="section" id="s-getting-the-current-domain-for-display">
<span id="getting-the-current-domain-for-display"></span><h3>Getting the current domain for display<a class="headerlink" href="#getting-the-current-domain-for-display" title="Permalink to this headline">¶</a></h3>
<p>LJWorld.com and Lawrence.com both have email alert functionality, which lets
readers sign up to get notifications when news happens. It&#8217;s pretty basic: A
reader signs up on a Web form and immediately gets an email saying,
&#8220;Thanks for your subscription.&#8221;</p>
<p>It&#8217;d be inefficient and redundant to implement this sign up processing code
twice, so the sites use the same code behind the scenes. But the &#8220;thank you for
signing up&#8221; notice needs to be different for each site. By using
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a>
objects, we can abstract the &#8220;thank you&#8221; notice to use the values of the
current site&#8217;s <a class="reference internal" href="#django.contrib.sites.models.Site.name" title="django.contrib.sites.models.Site.name"><tt class="xref py py-attr docutils literal"><span class="pre">name</span></tt></a> and
<a class="reference internal" href="#django.contrib.sites.models.Site.domain" title="django.contrib.sites.models.Site.domain"><tt class="xref py py-attr docutils literal"><span class="pre">domain</span></tt></a>.</p>
<p>Here&#8217;s an example of what the form-handling view looks like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.sites.shortcuts</span> <span class="kn">import</span> <span class="n">get_current_site</span>
<span class="kn">from</span> <span class="nn">django.core.mail</span> <span class="kn">import</span> <span class="n">send_mail</span>

<span class="k">def</span> <span class="nf">register_for_newsletter</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c"># Check form values, etc., and subscribe the user.</span>
    <span class="c"># ...</span>

    <span class="n">current_site</span> <span class="o">=</span> <span class="n">get_current_site</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
    <span class="n">send_mail</span><span class="p">(</span><span class="s">&#39;Thanks for subscribing to </span><span class="si">%s</span><span class="s"> alerts&#39;</span> <span class="o">%</span> <span class="n">current_site</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
        <span class="s">&#39;Thanks for your subscription. We appreciate it.</span><span class="se">\n\n</span><span class="s">-The </span><span class="si">%s</span><span class="s"> team.&#39;</span> <span class="o">%</span> <span class="n">current_site</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
        <span class="s">&#39;editor@</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">current_site</span><span class="o">.</span><span class="n">domain</span><span class="p">,</span>
        <span class="p">[</span><span class="n">user</span><span class="o">.</span><span class="n">email</span><span class="p">])</span>

    <span class="c"># ...</span>
</pre></div>
</div>
<p>On Lawrence.com, this email has the subject line &#8220;Thanks for subscribing to
lawrence.com alerts.&#8221; On LJWorld.com, the email has the subject &#8220;Thanks for
subscribing to LJWorld.com alerts.&#8221; Same goes for the email&#8217;s message body.</p>
<p>Note that an even more flexible (but more heavyweight) way of doing this would
be to use Django&#8217;s template system. Assuming Lawrence.com and LJWorld.com have
different template directories (<a class="reference internal" href="../settings.html#std:setting-TEMPLATE_DIRS"><tt class="xref std std-setting docutils literal"><span class="pre">TEMPLATE_DIRS</span></tt></a>), you could simply
farm out to the template system like so:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.core.mail</span> <span class="kn">import</span> <span class="n">send_mail</span>
<span class="kn">from</span> <span class="nn">django.template</span> <span class="kn">import</span> <span class="n">loader</span><span class="p">,</span> <span class="n">Context</span>

<span class="k">def</span> <span class="nf">register_for_newsletter</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c"># Check form values, etc., and subscribe the user.</span>
    <span class="c"># ...</span>

    <span class="n">subject</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s">&#39;alerts/subject.txt&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">Context</span><span class="p">({}))</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s">&#39;alerts/message.txt&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">Context</span><span class="p">({}))</span>
    <span class="n">send_mail</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="s">&#39;editor@ljworld.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">user</span><span class="o">.</span><span class="n">email</span><span class="p">])</span>

    <span class="c"># ...</span>
</pre></div>
</div>
<p>In this case, you&#8217;d have to create <tt class="file docutils literal"><span class="pre">subject.txt</span></tt> and <tt class="file docutils literal"><span class="pre">message.txt</span></tt>
template files for both the LJWorld.com and Lawrence.com template directories.
That gives you more flexibility, but it&#8217;s also more complex.</p>
<p>It&#8217;s a good idea to exploit the <a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a>
objects as much as possible, to remove unneeded complexity and redundancy.</p>
</div>
<div class="section" id="s-getting-the-current-domain-for-full-urls">
<span id="getting-the-current-domain-for-full-urls"></span><h3>Getting the current domain for full URLs<a class="headerlink" href="#getting-the-current-domain-for-full-urls" title="Permalink to this headline">¶</a></h3>
<p>Django&#8217;s <tt class="docutils literal"><span class="pre">get_absolute_url()</span></tt> convention is nice for getting your objects&#8217;
URL without the domain name, but in some cases you might want to display the
full URL &#8211; with <tt class="docutils literal"><span class="pre">http://</span></tt> and the domain and everything &#8211; for an object.
To do this, you can use the sites framework. A simple example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.contrib.sites.models</span> <span class="kn">import</span> <span class="n">Site</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span> <span class="o">=</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span><span class="o">.</span><span class="n">get_absolute_url</span><span class="p">()</span>
<span class="go">&#39;/mymodel/objects/3/&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Site</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_current</span><span class="p">()</span><span class="o">.</span><span class="n">domain</span>
<span class="go">&#39;example.com&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;http://</span><span class="si">%s%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">Site</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_current</span><span class="p">()</span><span class="o">.</span><span class="n">domain</span><span class="p">,</span> <span class="n">obj</span><span class="o">.</span><span class="n">get_absolute_url</span><span class="p">())</span>
<span class="go">&#39;http://example.com/mymodel/objects/3/&#39;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-enabling-the-sites-framework">
<span id="s-id1"></span><span id="enabling-the-sites-framework"></span><span id="id1"></span><h2>Enabling the sites framework<a class="headerlink" href="#enabling-the-sites-framework" title="Permalink to this headline">¶</a></h2>
<div class="versionchanged">
<span class="title">Changed in Django 1.6:</span> <p>In previous versions, the sites framework was enabled by default.</p>
</div>
<p>To enable the sites framework, follow these steps:</p>
<ol class="arabic">
<li><p class="first">Add <tt class="docutils literal"><span class="pre">'django.contrib.sites'</span></tt> to your <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><tt class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></tt></a>
setting.</p>
</li>
<li><p class="first">Define a <a class="reference internal" href="../settings.html#std:setting-SITE_ID"><tt class="xref std std-setting docutils literal"><span class="pre">SITE_ID</span></tt></a> setting:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">SITE_ID</span> <span class="o">=</span> <span class="mi">1</span>
</pre></div>
</div>
</li>
<li><p class="first">Run <a class="reference internal" href="../django-admin.html#django-admin-migrate"><tt class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></tt></a>.</p>
</li>
</ol>
<p><tt class="docutils literal"><span class="pre">django.contrib.sites</span></tt> registers a
<a class="reference internal" href="../signals.html#django.db.models.signals.post_migrate" title="django.db.models.signals.post_migrate"><tt class="xref py py-data docutils literal"><span class="pre">post_migrate</span></tt></a> signal handler which creates a
default site named <tt class="docutils literal"><span class="pre">example.com</span></tt> with the domain <tt class="docutils literal"><span class="pre">example.com</span></tt>. This site
will also be created after Django creates the test database. To set the
correct name and domain for your project, you can use a <a class="reference internal" href="../../topics/migrations.html#data-migrations"><em>data migration</em></a>.</p>
<p>In order to serve different sites in production, you&#8217;d create a separate
settings file with each <tt class="docutils literal"><span class="pre">SITE_ID</span></tt> (perhaps importing from a common settings
file to avoid duplicating shared settings) and then specify the appropriate
<span class="target" id="index-0"></span><a class="reference internal" href="../../topics/settings.html#envvar-DJANGO_SETTINGS_MODULE"><tt class="xref std std-envvar docutils literal"><span class="pre">DJANGO_SETTINGS_MODULE</span></tt></a> for each site.</p>
</div>
<div class="section" id="s-caching-the-current-site-object">
<span id="caching-the-current-site-object"></span><h2>Caching the current <tt class="docutils literal"><span class="pre">Site</span></tt> object<a class="headerlink" href="#caching-the-current-site-object" title="Permalink to this headline">¶</a></h2>
<p>As the current site is stored in the database, each call to
<tt class="docutils literal"><span class="pre">Site.objects.get_current()</span></tt> could result in a database query. But Django is a
little cleverer than that: on the first request, the current site is cached, and
any subsequent call returns the cached data instead of hitting the database.</p>
<p>If for any reason you want to force a database query, you can tell Django to
clear the cache using <tt class="docutils literal"><span class="pre">Site.objects.clear_cache()</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># First call; current site fetched from database.</span>
<span class="n">current_site</span> <span class="o">=</span> <span class="n">Site</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_current</span><span class="p">()</span>
<span class="c"># ...</span>

<span class="c"># Second call; current site fetched from cache.</span>
<span class="n">current_site</span> <span class="o">=</span> <span class="n">Site</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_current</span><span class="p">()</span>
<span class="c"># ...</span>

<span class="c"># Force a database query for the third call.</span>
<span class="n">Site</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span>
<span class="n">current_site</span> <span class="o">=</span> <span class="n">Site</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_current</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="s-the-currentsitemanager">
<span id="the-currentsitemanager"></span><h2>The <tt class="docutils literal"><span class="pre">CurrentSiteManager</span></tt><a class="headerlink" href="#the-currentsitemanager" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.contrib.sites.managers.CurrentSiteManager">
<em class="property">class </em><tt class="descclassname">managers.</tt><tt class="descname">CurrentSiteManager</tt><a class="headerlink" href="#django.contrib.sites.managers.CurrentSiteManager" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a> plays a key role in your
application, consider using the helpful
<a class="reference internal" href="#django.contrib.sites.managers.CurrentSiteManager" title="django.contrib.sites.managers.CurrentSiteManager"><tt class="xref py py-class docutils literal"><span class="pre">CurrentSiteManager</span></tt></a> in your
model(s). It&#8217;s a model <a class="reference internal" href="../../topics/db/managers.html"><em>manager</em></a> that
automatically filters its queries to include only objects associated
with the current <a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a>.</p>
<p>Use <a class="reference internal" href="#django.contrib.sites.managers.CurrentSiteManager" title="django.contrib.sites.managers.CurrentSiteManager"><tt class="xref py py-class docutils literal"><span class="pre">CurrentSiteManager</span></tt></a> by adding it to
your model explicitly. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib.sites.models</span> <span class="kn">import</span> <span class="n">Site</span>
<span class="kn">from</span> <span class="nn">django.contrib.sites.managers</span> <span class="kn">import</span> <span class="n">CurrentSiteManager</span>

<span class="k">class</span> <span class="nc">Photo</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">photo</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">FileField</span><span class="p">(</span><span class="n">upload_to</span><span class="o">=</span><span class="s">&#39;/home/photos&#39;</span><span class="p">)</span>
    <span class="n">photographer_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">pub_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
    <span class="n">site</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Site</span><span class="p">)</span>
    <span class="n">objects</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">()</span>
    <span class="n">on_site</span> <span class="o">=</span> <span class="n">CurrentSiteManager</span><span class="p">()</span>
</pre></div>
</div>
<p>With this model, <tt class="docutils literal"><span class="pre">Photo.objects.all()</span></tt> will return all <tt class="docutils literal"><span class="pre">Photo</span></tt> objects in
the database, but <tt class="docutils literal"><span class="pre">Photo.on_site.all()</span></tt> will return only the <tt class="docutils literal"><span class="pre">Photo</span></tt> objects
associated with the current site, according to the <a class="reference internal" href="../settings.html#std:setting-SITE_ID"><tt class="xref std std-setting docutils literal"><span class="pre">SITE_ID</span></tt></a> setting.</p>
<p>Put another way, these two statements are equivalent:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Photo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">site</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">SITE_ID</span><span class="p">)</span>
<span class="n">Photo</span><span class="o">.</span><span class="n">on_site</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
</pre></div>
</div>
<p>How did <a class="reference internal" href="#django.contrib.sites.managers.CurrentSiteManager" title="django.contrib.sites.managers.CurrentSiteManager"><tt class="xref py py-class docutils literal"><span class="pre">CurrentSiteManager</span></tt></a>
know which field of <tt class="docutils literal"><span class="pre">Photo</span></tt> was the
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a>? By default,
<a class="reference internal" href="#django.contrib.sites.managers.CurrentSiteManager" title="django.contrib.sites.managers.CurrentSiteManager"><tt class="xref py py-class docutils literal"><span class="pre">CurrentSiteManager</span></tt></a> looks for a
either a <a class="reference internal" href="../models/fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> called
<tt class="docutils literal"><span class="pre">site</span></tt> or a
<a class="reference internal" href="../models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> called
<tt class="docutils literal"><span class="pre">sites</span></tt> to filter on. If you use a field named something other than
<tt class="docutils literal"><span class="pre">site</span></tt> or <tt class="docutils literal"><span class="pre">sites</span></tt> to identify which
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a> objects your object is
related to, then you need to explicitly pass the custom field name as
a parameter to
<a class="reference internal" href="#django.contrib.sites.managers.CurrentSiteManager" title="django.contrib.sites.managers.CurrentSiteManager"><tt class="xref py py-class docutils literal"><span class="pre">CurrentSiteManager</span></tt></a> on your
model. The following model, which has a field called <tt class="docutils literal"><span class="pre">publish_on</span></tt>,
demonstrates this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib.sites.models</span> <span class="kn">import</span> <span class="n">Site</span>
<span class="kn">from</span> <span class="nn">django.contrib.sites.managers</span> <span class="kn">import</span> <span class="n">CurrentSiteManager</span>

<span class="k">class</span> <span class="nc">Photo</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">photo</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">FileField</span><span class="p">(</span><span class="n">upload_to</span><span class="o">=</span><span class="s">&#39;/home/photos&#39;</span><span class="p">)</span>
    <span class="n">photographer_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">pub_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
    <span class="n">publish_on</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Site</span><span class="p">)</span>
    <span class="n">objects</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">()</span>
    <span class="n">on_site</span> <span class="o">=</span> <span class="n">CurrentSiteManager</span><span class="p">(</span><span class="s">&#39;publish_on&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>If you attempt to use <a class="reference internal" href="#django.contrib.sites.managers.CurrentSiteManager" title="django.contrib.sites.managers.CurrentSiteManager"><tt class="xref py py-class docutils literal"><span class="pre">CurrentSiteManager</span></tt></a>
and pass a field name that doesn&#8217;t exist, Django will raise a <tt class="docutils literal"><span class="pre">ValueError</span></tt>.</p>
<p>Finally, note that you&#8217;ll probably want to keep a normal
(non-site-specific) <tt class="docutils literal"><span class="pre">Manager</span></tt> on your model, even if you use
<a class="reference internal" href="#django.contrib.sites.managers.CurrentSiteManager" title="django.contrib.sites.managers.CurrentSiteManager"><tt class="xref py py-class docutils literal"><span class="pre">CurrentSiteManager</span></tt></a>. As
explained in the <a class="reference internal" href="../../topics/db/managers.html"><em>manager documentation</em></a>, if
you define a manager manually, then Django won&#8217;t create the automatic
<tt class="docutils literal"><span class="pre">objects</span> <span class="pre">=</span> <span class="pre">models.Manager()</span></tt> manager for you. Also note that certain
parts of Django &#8211; namely, the Django admin site and generic views &#8211;
use whichever manager is defined <em>first</em> in the model, so if you want
your admin site to have access to all objects (not just site-specific
ones), put <tt class="docutils literal"><span class="pre">objects</span> <span class="pre">=</span> <span class="pre">models.Manager()</span></tt> in your model, before you
define <a class="reference internal" href="#django.contrib.sites.managers.CurrentSiteManager" title="django.contrib.sites.managers.CurrentSiteManager"><tt class="xref py py-class docutils literal"><span class="pre">CurrentSiteManager</span></tt></a>.</p>
</div>
<div class="section" id="s-site-middleware">
<span id="s-id2"></span><span id="site-middleware"></span><span id="id2"></span><h2>Site middleware<a class="headerlink" href="#site-middleware" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>If you often use this pattern:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.sites.models</span> <span class="kn">import</span> <span class="n">Site</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">site</span> <span class="o">=</span> <span class="n">Site</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_current</span><span class="p">()</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>there is simple way to avoid repetitions. Add
<a class="reference internal" href="../middleware.html#django.contrib.sites.middleware.CurrentSiteMiddleware" title="django.contrib.sites.middleware.CurrentSiteMiddleware"><tt class="xref py py-class docutils literal"><span class="pre">django.contrib.sites.middleware.CurrentSiteMiddleware</span></tt></a> to
<a class="reference internal" href="../settings.html#std:setting-MIDDLEWARE_CLASSES"><tt class="xref std std-setting docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></tt></a>. The middleware sets the <tt class="docutils literal"><span class="pre">site</span></tt> attribute on
every request object, so you can use <tt class="docutils literal"><span class="pre">request.site</span></tt> to get the current site.</p>
</div>
<div class="section" id="s-how-django-uses-the-sites-framework">
<span id="how-django-uses-the-sites-framework"></span><h2>How Django uses the sites framework<a class="headerlink" href="#how-django-uses-the-sites-framework" title="Permalink to this headline">¶</a></h2>
<p>Although it&#8217;s not required that you use the sites framework, it&#8217;s strongly
encouraged, because Django takes advantage of it in a few places. Even if your
Django installation is powering only a single site, you should take the two
seconds to create the site object with your <tt class="docutils literal"><span class="pre">domain</span></tt> and <tt class="docutils literal"><span class="pre">name</span></tt>, and point
to its ID in your <a class="reference internal" href="../settings.html#std:setting-SITE_ID"><tt class="xref std std-setting docutils literal"><span class="pre">SITE_ID</span></tt></a> setting.</p>
<p>Here&#8217;s how Django uses the sites framework:</p>
<ul class="simple">
<li>In the <a class="reference internal" href="redirects.html#module-django.contrib.redirects" title="django.contrib.redirects: A framework for managing redirects."><tt class="xref py py-mod docutils literal"><span class="pre">redirects</span> <span class="pre">framework</span></tt></a>, each
redirect object is associated with a particular site. When Django searches
for a redirect, it takes into account the current site.</li>
<li>In the comments framework, each comment is associated with a particular
site. When a comment is posted, its
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a> is set to the current site,
and when comments are listed via the appropriate template tag, only the
comments for the current site are displayed.</li>
<li>In the <a class="reference internal" href="flatpages.html#module-django.contrib.flatpages" title="django.contrib.flatpages: A framework for managing simple ?flat? HTML content in a database."><tt class="xref py py-mod docutils literal"><span class="pre">flatpages</span> <span class="pre">framework</span></tt></a>, each
flatpage is associated with a particular site. When a flatpage is created,
you specify its <a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a>, and the
<a class="reference internal" href="flatpages.html#django.contrib.flatpages.middleware.FlatpageFallbackMiddleware" title="django.contrib.flatpages.middleware.FlatpageFallbackMiddleware"><tt class="xref py py-class docutils literal"><span class="pre">FlatpageFallbackMiddleware</span></tt></a>
checks the current site in retrieving flatpages to display.</li>
<li>In the <a class="reference internal" href="syndication.html#module-django.contrib.syndication" title="django.contrib.syndication: A framework for generating syndication feeds, in RSS and Atom, quite easily."><tt class="xref py py-mod docutils literal"><span class="pre">syndication</span> <span class="pre">framework</span></tt></a>, the
templates for <tt class="docutils literal"><span class="pre">title</span></tt> and <tt class="docutils literal"><span class="pre">description</span></tt> automatically have access to a
variable <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">site</span> <span class="pre">}}</span></tt>, which is the
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a> object representing the current
site. Also, the hook for providing item URLs will use the <tt class="docutils literal"><span class="pre">domain</span></tt> from
the current <a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a> object if you don&#8217;t
specify a fully-qualified domain.</li>
<li>In the <a class="reference internal" href="../../topics/auth/index.html#module-django.contrib.auth" title="django.contrib.auth: Django's authentication framework."><tt class="xref py py-mod docutils literal"><span class="pre">authentication</span> <span class="pre">framework</span></tt></a>, the
<a class="reference internal" href="../../topics/auth/default.html#django.contrib.auth.views.login" title="django.contrib.auth.views.login"><tt class="xref py py-func docutils literal"><span class="pre">django.contrib.auth.views.login()</span></tt></a> view passes the current
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a> name to the template as
<tt class="docutils literal"><span class="pre">{{</span> <span class="pre">site_name</span> <span class="pre">}}</span></tt>.</li>
<li>The shortcut view (<tt class="docutils literal"><span class="pre">django.contrib.contenttypes.views.shortcut</span></tt>)
uses the domain of the current
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a> object when calculating
an object&#8217;s URL.</li>
<li>In the admin framework, the &#8220;view on site&#8221; link uses the current
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a> to work out the domain for the
site that it will redirect to.</li>
</ul>
</div>
<div class="section" id="s-requestsite-objects">
<span id="requestsite-objects"></span><h2><tt class="docutils literal"><span class="pre">RequestSite</span></tt> objects<a class="headerlink" href="#requestsite-objects" title="Permalink to this headline">¶</a></h2>
<p id="id3">Some <a class="reference internal" href="index.html"><em>django.contrib</em></a> applications take advantage of
the sites framework but are architected in a way that doesn&#8217;t <em>require</em> the
sites framework to be installed in your database. (Some people don&#8217;t want to,
or just aren&#8217;t <em>able</em> to install the extra database table that the sites
framework requires.) For those cases, the framework provides a
<a class="reference internal" href="#django.contrib.sites.requests.RequestSite" title="django.contrib.sites.requests.RequestSite"><tt class="xref py py-class docutils literal"><span class="pre">django.contrib.sites.requests.RequestSite</span></tt></a> class, which can be used as
a fallback when the database-backed sites framework is not available.</p>
<dl class="class">
<dt id="django.contrib.sites.requests.RequestSite">
<em class="property">class </em><tt class="descclassname">requests.</tt><tt class="descname">RequestSite</tt><a class="headerlink" href="#django.contrib.sites.requests.RequestSite" title="Permalink to this definition">¶</a></dt>
<dd><p>A class that shares the primary interface of
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a> (i.e., it has
<tt class="docutils literal"><span class="pre">domain</span></tt> and <tt class="docutils literal"><span class="pre">name</span></tt> attributes) but gets its data from a Django
<a class="reference internal" href="../request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><tt class="xref py py-class docutils literal"><span class="pre">HttpRequest</span></tt></a> object rather than from a database.</p>
<dl class="method">
<dt id="django.contrib.sites.requests.RequestSite.__init__">
<tt class="descname">__init__</tt>(<em>request</em>)<a class="headerlink" href="#django.contrib.sites.requests.RequestSite.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the <tt class="docutils literal"><span class="pre">name</span></tt> and <tt class="docutils literal"><span class="pre">domain</span></tt> attributes to the value of
<a class="reference internal" href="../request-response.html#django.http.HttpRequest.get_host" title="django.http.HttpRequest.get_host"><tt class="xref py py-meth docutils literal"><span class="pre">get_host()</span></tt></a>.</p>
</dd></dl>

<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.7: </span>This class used to be defined in <tt class="docutils literal"><span class="pre">django.contrib.sites.models</span></tt>. The
old import location will work until Django 1.9.</p>
</div>
</dd></dl>

<p>A <a class="reference internal" href="#django.contrib.sites.requests.RequestSite" title="django.contrib.sites.requests.RequestSite"><tt class="xref py py-class docutils literal"><span class="pre">RequestSite</span></tt></a> object has a similar
interface to a normal <a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a> object,
except its <a class="reference internal" href="#django.contrib.sites.requests.RequestSite.__init__" title="django.contrib.sites.requests.RequestSite.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a>
method takes an <a class="reference internal" href="../request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><tt class="xref py py-class docutils literal"><span class="pre">HttpRequest</span></tt></a> object. It&#8217;s able to deduce
the <tt class="docutils literal"><span class="pre">domain</span></tt> and <tt class="docutils literal"><span class="pre">name</span></tt> by looking at the request&#8217;s domain. It has
<tt class="docutils literal"><span class="pre">save()</span></tt> and <tt class="docutils literal"><span class="pre">delete()</span></tt> methods to match the interface of
<a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a>, but the methods raise
<a class="reference external" href="http://docs.python.org/3/library/exceptions.html#NotImplementedError" title="(in Python v3.4)"><tt class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></tt></a>.</p>
</div>
<div class="section" id="s-get-current-site-shortcut">
<span id="get-current-site-shortcut"></span><h2><tt class="docutils literal"><span class="pre">get_current_site</span></tt> shortcut<a class="headerlink" href="#get-current-site-shortcut" title="Permalink to this headline">¶</a></h2>
<p>Finally, to avoid repetitive fallback code, the framework provides a
<a class="reference internal" href="#django.contrib.sites.shortcuts.get_current_site" title="django.contrib.sites.shortcuts.get_current_site"><tt class="xref py py-func docutils literal"><span class="pre">django.contrib.sites.shortcuts.get_current_site()</span></tt></a> function.</p>
<dl class="function">
<dt id="django.contrib.sites.shortcuts.get_current_site">
<tt class="descclassname">shortcuts.</tt><tt class="descname">get_current_site</tt>(<em>request</em>)<a class="headerlink" href="#django.contrib.sites.shortcuts.get_current_site" title="Permalink to this definition">¶</a></dt>
<dd><p>A function that checks if <tt class="docutils literal"><span class="pre">django.contrib.sites</span></tt> is installed and
returns either the current <a class="reference internal" href="#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><tt class="xref py py-class docutils literal"><span class="pre">Site</span></tt></a>
object or a <a class="reference internal" href="#django.contrib.sites.requests.RequestSite" title="django.contrib.sites.requests.RequestSite"><tt class="xref py py-class docutils literal"><span class="pre">RequestSite</span></tt></a> object
based on the request.</p>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.7: </span>This function used to be defined in <tt class="docutils literal"><span class="pre">django.contrib.sites.models</span></tt>.
The old import location will work until Django 1.9.</p>
</div>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">The &#8220;sites&#8221; framework</a><ul>
<li><a class="reference internal" href="#example-usage">Example usage</a><ul>
<li><a class="reference internal" href="#associating-content-with-multiple-sites">Associating content with multiple sites</a></li>
<li><a class="reference internal" href="#associating-content-with-a-single-site">Associating content with a single site</a></li>
<li><a class="reference internal" href="#hooking-into-the-current-site-from-views">Hooking into the current site from views</a></li>
<li><a class="reference internal" href="#getting-the-current-domain-for-display">Getting the current domain for display</a></li>
<li><a class="reference internal" href="#getting-the-current-domain-for-full-urls">Getting the current domain for full URLs</a></li>
</ul>
</li>
<li><a class="reference internal" href="#enabling-the-sites-framework">Enabling the sites framework</a></li>
<li><a class="reference internal" href="#caching-the-current-site-object">Caching the current <tt class="docutils literal"><span class="pre">Site</span></tt> object</a></li>
<li><a class="reference internal" href="#the-currentsitemanager">The <tt class="docutils literal"><span class="pre">CurrentSiteManager</span></tt></a></li>
<li><a class="reference internal" href="#site-middleware">Site middleware</a></li>
<li><a class="reference internal" href="#how-django-uses-the-sites-framework">How Django uses the sites framework</a></li>
<li><a class="reference internal" href="#requestsite-objects"><tt class="docutils literal"><span class="pre">RequestSite</span></tt> objects</a></li>
<li><a class="reference internal" href="#get-current-site-shortcut"><tt class="docutils literal"><span class="pre">get_current_site</span></tt> shortcut</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="sitemaps.html">The sitemap framework</a></li>
    
    
      <li>Next: <a href="staticfiles.html">The staticfiles app</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">API Reference</a>
        
          <ul><li><a href="index.html"><tt class="docutils literal"><span class="pre">contrib</span></tt> packages</a>
        
        <ul><li>The &#8220;sites&#8221; framework</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/ref/contrib/sites.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="sitemaps.html" title="The sitemap framework">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="staticfiles.html" title="The staticfiles app">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>